package com.fresh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fresh.entity.FreshArea;
import com.fresh.mapper.AreaMapper;
import com.fresh.qo.AreaQO;
import com.fresh.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, FreshArea> implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public IPage<FreshArea> getByConditionAndPage(AreaQO areaQO) {
        //TODO 条件查询
        QueryWrapper<FreshArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(areaQO.getName()), "name", areaQO.getName());
        queryWrapper.eq(!StringUtils.isEmpty(areaQO.getLevel()), "level", areaQO.getLevel());
        //TODO 分页查询
        Page<FreshArea> areaPage = new Page<>(areaQO.getPageNum(), areaQO.getPageSize());
        IPage<FreshArea> areaIPage = areaMapper.selectPage(areaPage, queryWrapper);
        /*List<FreshArea> areaList = areaIPage.getRecords();
        for (FreshArea area :
                areaList) {
            if (area.getPid() != null) {
                FreshArea proArea = areaMapper.selectOne(new QueryWrapper<FreshArea>().eq("id", area.getPid()));
                //添加到前置地区中
                proArea.getSubAreas().add(area);
                //移除原来列表中的
                areaList.remove(area);
            }
        }
//        areaIPage.setRecords()*/
        return areaIPage;

    }

    /**
     * 目前可用，功能没问题
     * @param freshArea
     */
    @Override
    public void updateStatusById(FreshArea freshArea) {
        //id pid
        QueryWrapper<FreshArea> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<FreshArea> updateWrapper = new UpdateWrapper<>();

        //1
        if (freshArea.getLevel().equals(1)) {
            queryWrapper.eq("pid", freshArea.getId());
            List<FreshArea> son = areaMapper.selectList(queryWrapper);
            List<Integer> subIds = new ArrayList<>();
            for (FreshArea subArea :
                    son) {
                subArea.setStatus(freshArea.getStatus());
                subIds.add(subArea.getId());
                //更新二级地区
                areaMapper.updateById(subArea);
            }
            //得到二级所有地区ids
            queryWrapper.clear();
            queryWrapper.in("pid",subIds);
            List<FreshArea> grand = areaMapper.selectList(queryWrapper);
            for (FreshArea grandArea:
                    grand) {
                grandArea.setStatus(freshArea.getStatus());
                areaMapper.updateById(grandArea);
            }
        } else if (freshArea.getLevel().equals(2)) {
            queryWrapper.eq("pid", freshArea.getId());
            List<FreshArea> sub = areaMapper.selectList(queryWrapper);
            for (FreshArea subArea :
                    sub) {
                subArea.setStatus(freshArea.getStatus());
                //更新三级地区
                areaMapper.updateById(subArea);
            }
        }
        //改自己
        updateWrapper.clear();
        updateWrapper.eq("id", freshArea.getId()).set("status", freshArea.getStatus());
        areaMapper.update(null, updateWrapper);
    }

}
